#-*- encoding: utf-8 -*-
import win32com.client 
from Misc.Utils import *

from DataAccess.QueryBlp import *

from datetime import date,timedelta

from ReportTool.HldReport import *
from ReportTool.PnLReport import *
from ReportTool.NAVReport import *
from ReportTool.MktValReport import *
from ReportTool.TradeReport import *
from ReportTool.RealizedPnL import *
from ReportTool.OnshoreHldReport import *

from Config.ReportConfig import *
	
def Short_Report(ref_date):

	hld = query_hld(ref_date, portf_list=['SC028'])
	
	tplt_file = TPLT_PATH + 'Short Report.xls'
	xApp = win32com.client.Dispatch("Excel.Application")
	xBook = xApp.Workbooks.Open(tplt_file)
	xSht = xBook.Worksheets('Report')
	
	row = 2
	for elem in hld:
		if elem[2] == 'STOCK' and elem[3] == 'SHORT' and elem[17] == 'HKD':
			blp_ticker = hk_ticker_to_blp_ticker(elem[0])
			sec_info = query_blp_static([blp_ticker],['SHORT_NAME', 'EQY_SH_OUT'])
			
			if abs(elem[9]) > 30000000 or abs(elem[4])/sec_info[blp_ticker]['EQY_SH_OUT']/1000000 > 0.0002:
				xSht.Cells(row, 1).Value = 'EUSMDDKB5W'
				xSht.Cells(row, 2).Value = 'Springs China Opportunities Master Fund'
				xSht.Cells(row, 3).Value = elem[0]
				xSht.Cells(row, 4).Value = sec_info[blp_ticker]['SHORT_NAME']
				xSht.Cells(row, 5).Value = abs(elem[4])
				xSht.Cells(row, 6).Value = abs(elem[9])
				row += 1
		#HI: Hang Seng Index Futures, HC: H-shares Index Futures
		if	elem[2] == 'FUTURES' and (elem[0][:2] == 'HI' or elem[0][:2] == 'HC') and abs(elem[4]) >= 500:
			xSht.Cells(row, 1).Value = 'EUSMDDKB5W'
			xSht.Cells(row, 2).Value = 'Springs China Opportunities Master Fund'
			xSht.Cells(row, 3).Value = elem[0]
			xSht.Cells(row, 4).Value = query_blp_static([elem[0]],['SHORT_NAME'])[elem[0]]['SHORT_NAME']
			xSht.Cells(row, 5).Value = elem[4]
			row += 1
	
	rept_file = REPT_PATH + ref_date.strftime("%Y%m%d") + 'SC.csv'
	xBook.SaveAs(rept_file, win32com.client.constants.xlCSV)
	xBook.Close()
	del xApp
	
	print 'info, short report completed at ', ref_date.isoformat()